package com.yummy.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yummy.dto.DishDto;
import com.yummy.dto.QueryDishVo;
import com.yummy.entity.Category;
import com.yummy.entity.Dish;
import com.yummy.entity.DishFlavor;
import com.yummy.mapper.CategoryMapper;
import com.yummy.mapper.DishFlavorMapper;
import com.yummy.mapper.DishMapper;
import com.yummy.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author buding
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;

    @Autowired
    DishFlavorMapper dishFlavorMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Override

    public PageInfo<DishDto> queryByPageAndCondition(QueryDishVo queryDishVo) {
        //查询所有
        Page<DishDto> page = PageHelper.startPage(queryDishVo.getCurrentPage(), queryDishVo.getPageSize());

        dishMapper.selectAllByCondition(queryDishVo);

        PageInfo<DishDto> dishPageInfo = page.toPageInfo();

        List<DishDto> dishList = dishPageInfo.getList();

        ArrayList<DishDto> dishDtos = new ArrayList<>();

        for (int i = 0; i < dishList.size(); i++) {

            DishDto dishDto = JSON.parseObject(JSON.toJSONString(dishList.get(i)), DishDto.class);
            //根据  categoryId 填充 categoryName |
            Category category = categoryMapper.selectByPrimaryKey(dishDto.getCategoryId());
            dishDto.setCategoryName(category.getName());
            //根据 id 填充 List<DishFlavor> flavors
            List<DishFlavor> dishFlavor = dishFlavorMapper.selectDishFlavorByDishId(queryDishVo.getId());
            dishDto.setFlavors(dishFlavor);
            dishDtos.add(dishDto);
        }
        dishPageInfo.setList(dishDtos);

        return dishPageInfo;
    }

    @Override
    public boolean addDish(Dish dish) {
        return dishMapper.insertDish(dish)>0;
    }


    @Override
    public boolean editDish(Dish dish) {
        return dishMapper.updateByDish(dish)>0;
    }

    @Override
    public boolean removeDishs(String... ids) {
       return dishMapper.deleteByDishPrimaryKeys(ids)>0;
    }
    //停售
    @Override
    public boolean updateStatus(String id, String status) {

        return dishMapper.updateStatusById(id,status)>0;
    }

    @Override
    public Dish findDishById(String id) {
        Dish dish = dishMapper.selectByDishId(id);
        return dish;
    }

    @Override
    @Transactional
    public boolean addDishDto(DishDto dishDto) {
        //写入dish
        boolean dishResult = dishMapper.insertDishDto(dishDto)>0;

        //获取菜单id并且写入flavor
        long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(favor->{favor.setDishId(dishId);});
        boolean flavorResult = dishFlavorMapper.insertBatch(flavors)>0;

        return dishResult&flavorResult;
    }

    /**
     * 根据categoryId查询菜品
     * @param categoryId
     */
    @Override
    public List<Dish> findDishByCategoryId(Long categoryId) {

        List<Dish> _dishes = dishMapper.selectAll();

        ArrayList<Dish> dishes = new ArrayList<>();
        for (Dish dish : _dishes) {
            if (categoryId.equals(dish.getCategoryId())) {
                dishes.add(dish);
            }
        }


        return dishes;
    }
}
